package database;

import User.User;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class MyDB {
    // 数据库连接信息
    private static final String URL = "jdbc:mysql://localhost:3306/client_db";
    private static final String USERNAME = "root";
    private static final String PASSWORD = ""; // 尝试使用空密码
    
    // 静态代码块加载驱动
    static {
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    
    // 获取数据库连接
    public Connection getConn() {
        Connection conn = null;
        try {
            conn = DriverManager.getConnection(URL, USERNAME, PASSWORD);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return conn;
    }
    
    // 用户登录验证
    public boolean validateUser(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = getConn();
            String sql = "SELECT id FROM users WHERE name = ? AND pwd = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, user.getName());
            pstmt.setString(2, user.getPwd());
            
            rs = pstmt.executeQuery();
            
            if (rs.next()) {
                // 设置用户ID
                user.setId(rs.getInt("id"));
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    
    // 用户注册
    public boolean addUser(User user) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        
        try {
            conn = getConn();
            
            // 先检查用户名是否已存在
            String checkSql = "SELECT id FROM users WHERE name = ?";
            pstmt = conn.prepareStatement(checkSql);
            pstmt.setString(1, user.getName());
            rs = pstmt.executeQuery();
            
            if (rs.next()) {
                // 用户名已存在
                return false;
            }
            
            // 关闭当前的结果集和预处理语句
            rs.close();
            pstmt.close();
            
            // 插入新用户
            String insertSql = "INSERT INTO users (name, pwd) VALUES (?, ?)";
            pstmt = conn.prepareStatement(insertSql, PreparedStatement.RETURN_GENERATED_KEYS);
            pstmt.setString(1, user.getName());
            pstmt.setString(2, user.getPwd());
            
            int rowsAffected = pstmt.executeUpdate();
            
            if (rowsAffected > 0) {
                // 获取生成的用户ID
                rs = pstmt.getGeneratedKeys();
                if (rs.next()) {
                    user.setId(rs.getInt(1));
                }
                return true;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            try {
                if (rs != null) rs.close();
                if (pstmt != null) pstmt.close();
                if (conn != null) conn.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}